﻿using System;
using System.Collections.Generic;

using System.Text;
using System.IO;
using System.Configuration;


namespace SQLDeployActions
{
    public class DeployActions
    {
        internal static void LoadAppSettings(string[] args)
        {
           DeployConstants.SQLSequenceFile = ConfigurationManager.AppSettings["SQLSequenceFile"];
           DeployConstants.BaseSource = ConfigurationManager.AppSettings["BaseSource"];
           DeployConstants.DMLFolderName = ConfigurationManager.AppSettings["DMLFolderName"];
           if (!DeployConstants.IsTrustedConnection)
           {
               DeployConstants.UserName = args[2];
               DeployConstants.Password = args[3];
           }
           DeployConstants.DBName = args[1];
           DeployConstants.ServerName = args[0];
        }



        public static void CreateSQLRunBatch()
        {
            string[] FilePaths = LoadFilePaths(DeployConstants.SQLSequenceFile);
            if (FilePaths != null && FilePaths.Length > 0)
            {
                foreach (string path in FilePaths)
                {
                    DeployActionExecute.ExecuteSQLFile(path, (path.ToLower().StartsWith(DeployConstants.DMLFolderName)));
                }
            }
            else
            {
                throw new DeployException(DeployConstants.MSG_SEQUENCE_FILE_EMPTY);
            }
        }

        private static string[] LoadFilePaths(string orderFilePath)
        {
            string[] FilePaths = null;
            List<string> AllPaths=null;
            StreamReader ddlReader = new StreamReader(orderFilePath);
            String RawFilePath = ddlReader.ReadToEnd();
            ddlReader.Close();
            ddlReader.Dispose();
            if (RawFilePath.Length > 0)
            {
                FilePaths = RawFilePath.Split(new string[] { System.Environment.NewLine }
                                                , StringSplitOptions.RemoveEmptyEntries);
                if (FilePaths != null && FilePaths.Length > 0)
                {
                    /*
                     * A better way to implement this logic is to use LINQ to Object to filter out
                     * paths which are not commented. But to keep this backward compatible with Framework 2.0
                     * we need to loop thorugh the array to remove any commented paths. This should not be a significant
                     * performance leak even if it contains a few hundered entries.
                     * All comments in Sequence File should start with ##
                     */
                    AllPaths=new List<string>(FilePaths);
                    for(int lastIndex=AllPaths.Count-1;lastIndex>=0;lastIndex--)
                        if(AllPaths[lastIndex].StartsWith(DeployConstants.COMMENT_SYMBOL))
                            AllPaths.RemoveAt(lastIndex);
                    FilePaths = AllPaths.ToArray();
                }
            }
            return FilePaths;
        }
    }
}
